﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain.IntegrityCheck
{
    public class DataRowCountIC : IIntegrityCheck
    {
        public readonly static Guid ID = new Guid("{C2A737A5-0CA5-480F-B229-CC60F0D04C32}");

        public const string NAME = "数据行数验证插件";
        public const string DESCRIPTION = "检查数据总数目是否符合要求，如果总数不再预定范围内则视为有错误。";

        public bool Validate(DataServiceInterface.IData data, Dictionary<string, string> parameters, out string message)
        {
            int min = 0;
            int max = 0;
            try
            {
                string strMin = parameters["min"];
                string strMax = parameters["max"];
                int.TryParse(strMin, out min);
                int.TryParse(strMax, out max);
            }
            catch(Exception ex)
            {
                message = ex.Message;
                return false;
            }
            if(data.RowCount>=min && data.RowCount<=max)
            {
                message = null;
                return true;
            }
            else
            {
                message = string.Format("数据总数:{0},不在范围内({1},{2})", data.RowCount, min, max);
                return false;
            }
        }

        public List<Parameter> EnumParameters()
        {
            List<Parameter> parameters = new List<Parameter>();
            parameters.Add(new Parameter("min", "最小值", typeof(int)));
            parameters.Add(new Parameter("max", "最大值", typeof(int)));
            return parameters;
        }

        public string GetConditionText(Dictionary<string, string> parameters)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("数据总数在范围[{0},{1}]", parameters["min"], parameters["max"]);
            return builder.ToString();
        }
    }
}
